package com.tedu.controller;


import com.tedu.dao.EmpDao;
import com.tedu.pojo.Emp;
import com.tedu.pojo.JsonResult;
import com.tedu.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 控制层
 */
@Controller
public class EmpController {
    @Autowired
    private EmpDao empDao;
    @Autowired
    private EmpService empService;
    /**
     * 主页
     */
    @RequestMapping("/doindex")
    public String doindex() {

        return "emplist";
    }

    /**
     * 下载Emp表
     */
    @GetMapping("/empDownload")
    public void salaryTemplate(HttpServletResponse response) throws Exception {
        empService.EmpTable(response);
    }

    /**
     * 查询Emp集合，传递到客户端
     */
    @RequestMapping("/doempList")
    @ResponseBody
    public List<Emp> doempList() {
        List<Emp> emp = empDao.findEmp();
        return  emp;
    }

    //ID删除Emp,转发到查询页面
    @RequestMapping("/empDele")
    public String empDele(Integer ID) {
        System.out.println(ID);
        empDao.deleteEmpById(ID);
        return "forward:doindex";
    }

    //ID查询Emp,转到修改页面
    @RequestMapping("/empFindById")
    public String empFindById(Integer ID, Model model) {
        Emp emp = empDao.findEmpById(ID);
        model.addAttribute("emp", emp);
        return "updateEmp";
    }

    //修改Emp，转发到查询页面
    @RequestMapping("/empUpdate")
    public String empUpdate(Emp emp) {
        System.out.println(emp);
        empDao.updateEmpById(emp);
        return "forward:doindex";
    }

    //增加Emp，转发到查询页面
    @RequestMapping("/empAdd")
    public String empAdd(Emp emp) {
        empDao.addEmp(emp);
        return "forward:doindex";
    }


    //通用页面跳转方法
    @RequestMapping("/{htmlName}")
    public String toJspPage(@PathVariable String htmlName) {

        return htmlName;
    }


}
